<template>
  <div class="container" v-if="!showDetail">
    <div class="header">
      <div class="title">Banner列表</div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="id" width="80"/>
      <el-table-column prop="img" label="图片" width="200">
        <el-image
            v-if="scope.row.img"
            slot-scope="scope"
            :src="scope.row.img"
            style="max-height: 50px; max-width: 120px"/>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="120"/>
      <el-table-column prop="title" label="标题" width="150"/>
      <el-table-column :show-overflow-tooltip="true" prop="description" label="描述" width="150"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="handleDetail(scope.row)">查看</el-button>
          <el-button type="danger" plain size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination :page-count="pageCount" @current-change="handleChangePage"  :background="true"></el-pagination>
    </div>
  </div>
  <banner-detail :bannerId="bannerDetailId" v-else @detail-close="onDetailClose"> </banner-detail>
</template>

<script>
import Banner from '@/model/banner'
import BannerDetail from '@/view/banner/banner-detail'

export default {
  name: 'banner-list',
  components: { BannerDetail },
  data () {
    return {
      showDetail: false,
      tableData: [],
      bannerDetailId: -1,
      currentPage: 1,
      pageCount: 1,
      total: 0
    }
  },
  created () {
    this.getBanners()
  },
  methods: {
    async getBanners () {
      const page = this.currentPage - 1
      const res = await Banner.getBanners(page, )
      this.total = res.total
      this.pageCount = this.calPageCount(res.total, 10)
      this.tableData = res.items
    },
    async getBannersAfterDelete () {
      const page = this.currentPage - 1
      const res = await Banner.getBanners(page)
      this.pageCount = this.calPageCount(this.total, 10)
      this.tableData = res.items
    },
    calPageCount (total, perPageCount) {
      return Math.ceil(total / perPageCount)
    },
    handleChangePage (val) {
      console.log(val)
      this.currentPage = val
      this.getBanners()
    },
    handleDelete (val) {
      this.$confirm('此操作将永久删除该banner，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await Banner.delete(val.id)
          this.getBannersAfterDelete()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleDetail (event) {
      this.bannerDetailId = event.id
      this.showDetail = true
    },
    onDetailClose () {
      this.showDetail = false
    }
  }

}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px 30px 20px 30px;
  .title {
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 20px;
    color: $theme;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin: 20px;
}
</style>
